﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SEO.Model;
using System.Data.SQLite;
using System.Data.SqlClient;

namespace SEO.DAL
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 电子邮件表
    /// 完成日期：2011年9月15日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class ESEmailsDAL
    {

        /// <summary>
        /// 获取ESEmails表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<ESEmails> GetAllESEmailsDAL()
        {
            List<ESEmails> list = new List<ESEmails>();
            string strSql = "select * from Emails";
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    ESEmails email = new ESEmails();
                    email.EmailID = int.Parse(dr["email_id"].ToString());
                    email.EmailAddress = dr["email_address"].ToString();
                    email.Keywords = dr["keywords"].ToString();
                    email.CatchTime = dr["catch_time"].ToString();
                    email.EmailSource = dr["email_source"].ToString();
                    email.WebName = dr["web_name"].ToString();

                    list.Add(email);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定ESEmails -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取ESEmails表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<ESEmails> GetESEmailsByWhereDAL(string strSql)
        {
            List<ESEmails> list = new List<ESEmails>();
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    ESEmails email = new ESEmails();
                    email.EmailID = int.Parse(dr["email_id"].ToString());
                    email.EmailAddress = dr["email_address"].ToString();
                    email.Keywords = dr["keywords"].ToString();
                    email.CatchTime = dr["catch_time"].ToString();
                    email.EmailSource = dr["email_source"].ToString();
                    email.WebName = dr["web_name"].ToString();

                    list.Add(email);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定SQL语句获取ESEmails表中数据 -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往ESEmails表中插入数据
        /// <param name="email">实例化ESEmails类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertESEmailsDAL(SEO.Model.ESEmails email)
        {
            int iNum = 0;
            string strSql = "insert into Emails(email_id,email_address,keywords,catch_time,email_source,web_name) " +
             " values(" + email.EmailID + ",'" + email.EmailAddress + "','" + email.Keywords + "',datetime('now','localtime'),'" + email.EmailSource + "','" + email.WebName + "')";
            try
            {
                iNum = SEO.DBSqlHelp.ESDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("往ESEmails表中插入数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除ESEmails表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteESEmailsDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.ESDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定条件删除ESEmails表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新ESEmails表中数据
        /// </summary>
        /// <param name="ks">实例化ESEmails类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateESEmailsDAL(SEO.Model.ESEmails email)
        {
            int iNum = 0;
            string strSql = "update ESEmails set email_address=" + email.EmailAddress + " where email_id=" + email.EmailID;
            try
            {
                iNum = SEO.DBSqlHelp.ESDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新ESEmails表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表ESEmails到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindESEmailsDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "Emails");
                ds.Tables["Emails"].Columns.Add("ViewCatch", typeof(string));
                for (int i = 0; i < ds.Tables["Emails"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["Emails"].Rows[i];
                }
                dgv.DataSource = ds.Tables["Emails"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定ESEmails -> " + ex.Message);
            }
        }

        /// <summary>
        /// 获取表中最大ID，并自增1
        /// </summary>
        /// <returns>返回表中最大ID，并自增1</returns>
        public int GetAutoAddNum()
        {
            int iNum = 0;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select email_id from Emails order by email_id desc limit 1";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    iNum = int.Parse(dr["email_id"].ToString()) + 1;
                }
                else
                {
                    iNum = 1;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("获取表中最大ID，并自增1 -> " + ex.Message);
            }

            return iNum;
        }

        /// <summary>
        /// 检测是否已存在数据库中，屏蔽已抓取的数据
        /// </summary>
        /// <param name="strEmail">电子邮件</param>
        /// <returns>返回 True or False</returns>
        public bool CheckEmailAddressIsExist(string strEmail)
        {
            bool bValue = false;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select email_id from Emails where email_address='" + strEmail + "'";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    bValue = true;
                }
                else
                {
                    bValue = false;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("检测是否已存在数据库中 -> " + ex.Message);
            }

            return bValue;
        }


        /// <summary>
        /// 绑定表ESEmails到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindServerEmailsDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                string strConn = "server=222.73.68.28;uid=cloudin;pwd=cloudin!@#53344521;database=CloudinEmails;pooling=true";
                SqlConnection cn = new SqlConnection(strConn);
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "Emails");
                ds.Tables["Emails"].Columns.Add("ViewCatch", typeof(string));
                for (int i = 0; i < ds.Tables["Emails"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["Emails"].Rows[i];
                }
                dgv.DataSource = ds.Tables["Emails"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定ESEmails -> " + ex.Message);
            }
        }
    }
}
